$(function () {
    var markid = getUrlString('markid');
    // var markid = 'b88afc62e44094ff46749eb4b55cfadc';
    var page = -1,totalSize = 0,sort= 11;

    // var mRefresh = new MiniRefresh({
    //     container: '#mrefresh', //这里是插件的容器ID
    //     down: {
    //         isAuto: false,
    //         callback: function () {
    //             // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
    //             // $('#cashCommodityList').empty();//清空容器
    //             totalSize = 0;
    //             page = -1;
    //             $('.check,.noCheckAll').hide();
    //             $('.cashCommodityList li').removeClass('selected');
    //             $('.kuid').removeClass('forId');
    //             $('.checkAllSelected').show();
    //             var checkNum = $('.forId').length;
    //             $('#checkNum').html('('+checkNum+')');
    //             mRefresh.endDownLoading();
    //         }
    //     }
    //     ,
    //     up: {
    //         isAuto: true,
    //         callback: function () {
    //             // 向上拉后的回调事件
    //             page += 1;
    //             cashCommodityList(page,sort);
    //
    //         }
    //     }
    // });
    cashCommodityList(0,sort);
    function cashCommodityList (page,sort) {
        layLoading();
        var data = {
            'markid':markid,
            'mainstate':1,
            'type':0,
            'sort':sort,
            'page':page
        };
        getData('post','/niu/apiv3/niu_getuserkuv3_full.php',data,function (data) {
            console.log(data);
            layer.closeAll('loading');
            if(data.resultcode == 0 ){
                var append = '';

                $(data.data).each(function () {
                    if (this.lockon === '0'){
                        append += '<li>' +
                            '            <input class="kuid" type="hidden" value="'+this.id+'">'+
                            '            <div class="pic">' +
                            '                <img src="'+this.pics+'-toysbox11'+'" alt="">' +
                            '                <img class="noCheck" src="../../res/img/my/myEggPool/noCheck.png" alt="">' +
                            '                <img class="check" src="../../res/img/my/myEggPool/check.png" alt="">' +
                            '                        <div class="dayNum">'+this.outtime+'</div>' +
                            '            </div>' +
                            '            <div class="name">'+this.name+'</div>' +
                            '        </li>'
                    }

                });
                $('#cashCommodityList').append(append);
                $('#loaded').show();
                //点击选择商品
                $('#cashCommodityList li').click(function () {
                    if($(this).hasClass('selected')){
                        $(this).removeClass('selected');
                        $(this).find('.pic').children('.check').hide();
                        $(this).children('input').removeClass('forId');
                        var checkNum = $('.forId').length;
                        $('#checkNum').html('('+checkNum+')')
                    }else {
                        $(this).addClass('selected');
                        $(this).find('.pic').children('.check').show();
                        $(this).children('input').addClass('forId');
                        var checkNum = $('.forId').length;
                        $('#checkNum').html('('+checkNum+')')
                    }
                });

                //全选
                $('.checkAllSelected').click(function () {
                    $(this).hide();
                    $('.cashCommodityList li').addClass('selected');
                    $('.kuid').addClass('forId');
                    $('.check,.noCheckAll').show();
                    var checkNum = $('.forId').length;
                    $('#checkNum').html('('+checkNum+')')
                });

                //取消全选
                $('.noCheckAll').click(function () {
                    $('.check,.noCheckAll').hide();
                    $('.cashCommodityList li').removeClass('selected');
                    $('.kuid').removeClass('forId');
                    $('.checkAllSelected').show();
                    var checkNum = $('.forId').length;
                    $('#checkNum').html('('+checkNum+')')
                });
                // mRefresh.endUpLoading(true);
            }else {
                laymsg(data.msg)
            }
        })
    }


    $('.pickUpBtn').click(function () {
        var pickUpArr = [];
        $('.forId').each(function () {
            pickUpArr.push($(this).val())
        });
        var pickUpStr = pickUpArr.join(';');
        if (pickUpStr === ''){
            laymsg('请选择发货商品')
        } else {
            checktoys(pickUpStr);
        }

    });

    $('.cancel').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    function checktoys(kutoysids) {
        var data = {
            'markid':markid,
            'kutoysids':kutoysids
        };
        getData('post','/niu/api/fahuo_checktoys.php',data,function (data) {
            console.log(data);
            if(data.resultcode == 0 ){

                let kutoysid = kutoysids.replace(/;/g,',');
                console.log(kutoysid);
                window.location.href = 'dancool://web='+urlView+'shipments/shipmentsOrder.html?kutoysids='+kutoysid+'&markid='+markid
                // alert(urlView+'shipments/shipmentsOrder.html?kutoysids='+kutoysids+'&markid='+markid)
                // window.location.href = 'dancool://addfahuo='+kutoysids
            }else {
                laymsg(data.msg)
            }
        })
    }


});
